/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: ListMenuView.java 47 2008-05-16 21:01:33Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

public class ListMenuView implements MenuView
{
    private float _iconScale = 2.0f;
    private int _listRows;
    private int _listColumns;
    private int _listItemHeight;
    
    public ListMenuView(int rows, int columns, int itemHeight)
    {
        _listColumns = columns;
        _listRows = rows;
        _listItemHeight = itemHeight;

    }

    public int getItemPageOffsetAt(BasicMenuFrame menu, int x, int y)
    {
        int ix = x / (menu.getWidth() / _listColumns);
        int iy = y / _listItemHeight;
        return ix * _listRows + iy;
    }

    public int getItemXPosition(BasicMenuFrame menu, int itemPageOffset)
    {
        return (itemPageOffset / _listRows) *
               (menu.getWidth() / _listColumns);
    }

    public int getItemYPosition(BasicMenuFrame menu, int itemPageOffset)
    {
        return (itemPageOffset % _listRows) * _listItemHeight;
    }

    public int getItemsPerPage(BasicMenuFrame menu)
    {
        return _listRows * _listColumns;
    }
    
    public int getRowsPerPage(BasicMenuFrame menu)
    {
        return _listRows;
    }

    public void renderMenuItem(BasicMenuFrame menu, MenuItem item, Graphics g,
            int screenX, int screenY, boolean highlighted)
    {
        int itemWidth = menu.getWidth() / _listColumns;

        if (highlighted && !item.isDisabled()) {
            g.setColor(menu.getBackgroundColor());
            g.fillRect(screenX, screenY, 
                       itemWidth + 1, _listItemHeight + 1);
        }
        
        Image iconImage = item.getIconImage(); 
        int offsetX = 0;
        if (iconImage != null) {
            iconImage.draw(screenX + offsetX, screenY, _iconScale);
            offsetX += iconImage.getWidth() * _iconScale;
        }

        int offsetY = (_listItemHeight - g.getFont().getLineHeight()) / 2;
        if (item.isDisabled()) {
            g.setColor(Color.gray);
        } else {
            g.setColor(menu.getForegroundColor());
        }
        g.drawString(
                item.getText().toUpperCase(),
                screenX + offsetX, screenY + offsetY
        );
        
    }
}